let commonClass = {
    //时间戳转字符串
    dateTimeString: function (timestamp) {
        if (timestamp == null || timestamp == 0) {
            return "";
        }
        const time = new Date();
        time.setTime(timestamp);
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const d = time.getDate();
        const h = time.getHours();
        const mm = time.getMinutes();
        const s = time.getSeconds();
        return y + '-' + this.s(m) + '-' + this.s(d) + ' ' + this.s(h) + ':' + this.s(mm) + ':' + this.s(s);
    },
    //时间戳转字符
    dateString: function (timestamp) {
        if (timestamp == null || timestamp == 0) {
            return "";
        }
        const time = new Date();
        time.setTime(timestamp);
        const y = time.getFullYear();
        const m = time.getMonth() + 1;
        const d = time.getDate();
        return y + '-' + this.s(m) + '-' + this.s(d);
    },
    s: function (m) {
        return m < 10 ? '0' + m : m
    },
    //获取路径中带的query参数
    getQueryString: function (name) {
        const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        const r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        }
        return null;
    },
    //获取路径中的第index个参数值
    getPathVariable: function (index) {
        const url = window.location.href;

        let arr = url.split("/");
        console.log(arr);

        console.log(arr[arr.length - index]);
        return arr[arr.length - index];
    },
    //验证字符串中是否有非法字符
    stringValid: function (str) {
        //过滤非法字符
        const invalid = /[`~!@#$%^&*_<>?:"{},\/;'[\]]/im;
        return invalid.test(str);
    },
    //验证只能含有字母和数字
    validNumAndEn: function (str, min, max) {
        const pattern = '^[0-9a-zA-Z]{' + min + ',' + max + '}$';
        const regex = new RegExp(pattern);
        if (str.match(regex)) {
            return true;
        } else {
            return false;
        }
    },
    //验证手机号的正确性
    validPhone: function (str) {
        if (!(/^1[345789]\d{9}$/.test(str))) {
            return false;
        }
        return true;
    },
    //验证手机是否符合年份
    validMobile: function (str) {
        const ok = /\d(6,15)$/;
        return ok.test(str);
    },
    //获取当前年份
    getYear: function (timestamp) {
        let time = new Date();
        time.setTime(timestamp);
        return time.getFullYear();
    },
    //验证email是否符合规定
    validEmail: function (str) {
        const reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        return reg.test(str);
    },
    //转成百分数
    floatPrecious: function (shouldBe, absentBe, precious) {
        const floatShouldBe = parseFloat(shouldBe);
        const floatAbsentlBe = parseFloat(absentBe);
        const result = ((floatShouldBe - floatAbsentlBe) / floatShouldBe) * 100;
        return result.toFixed(precious);
    },
    //按照逗号把数组转换为一个个标签 因为多个地方使用了
    splitArray: function (str) {
        if (str == null || str.length == 0) {
            return "";
        }
        let array = str.split(",");
        let html = "";
        for (let i in array) {
            let item = array[i];
            html += item + "<br>";
        }
        return html;
    },
    //base64转图片
    base64ToImage: function (dataUrl) {
        if (dataUrl == null || dataUrl.length == 0) {
            return null;
        }
        let arr = dataUrl.split(',');
        if (arr == null || arr.length == 0) {
            return null;
        }
        let a = arr[0].match(/:(.*?);/);
        if (a == null || a.length == 0) {
            return null;
        }
        let mime = a[1];
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], {type: mime});
    },
    //根据用户填写的邮件跳转到相应的邮箱登录网页
    jumpToEmailLogin: function (email) {
        const hash = {
            'qq.com': 'http://mail.qq.com',
            'gmail.com': 'http://mail.google.com',
            'sina.com': 'http://mail.sina.com.cn',
            '163.com': 'http://mail.163.com',
            '126.com': 'http://mail.126.com',
            'yeah.net': 'http://www.yeah.net/',
            'sohu.com': 'http://mail.sohu.com/',
            'tom.com': 'http://mail.tom.com/',
            'sogou.com': 'http://mail.sogou.com/',
            '139.com': 'http://mail.10086.cn/',
            'hotmail.com': 'http://www.hotmail.com',
            'live.com': 'http://login.live.com/',
            'live.cn': 'http://login.live.cn/',
            'live.com.cn': 'http://login.live.com.cn',
            '189.com': 'http://webmail16.189.cn/webmail/',
            'yahoo.com.cn': 'http://mail.cn.yahoo.com/',
            'yahoo.cn': 'http://mail.cn.yahoo.com/',
            'eyou.com': 'http://www.eyou.com/',
            '21cn.com': 'http://mail.21cn.com/',
            '188.com': 'http://www.188.com/',
            'foxmail.com': 'http://www.foxmail.com',
            'outlook.com': 'http://www.outlook.com'
        };

        let _email = email.split('@')[1];    //获取邮箱域
        if (hash.hasOwnProperty(_email)) {
            swal({
                    title: "邮件发送成功",
                    text: "已经向您的邮箱中发送了一封邮件，您需要跳转到登录邮件界面吗！",
                    type: "success",
                    showCancelButton: true,
                    confirmButtonText: "好的",
                    cancelButtonText: "不用了",
                    closeOnConfirm: true,
                    closeOnCancel: true
                },
                function (isConfirm) {
                    if (isConfirm) {
                        window.open(hash[_email]);
                    }
                });
        } else {
            layer.msg("激活邮件已经发送到您的邮箱，请在两小时内完成操作，否则链接将会失效");
        }
    },
    //发送完邮件或者短信后  按钮显示倒计时
    countDown: function (count, btnName, word) {
        $("#" + btnName).attr("disabled", true);
        $("#" + btnName).html(word + "(" + count + ")");
        count--;
        let flag = setInterval(function () {
            if (count == 0) {
                $("#" + btnName).removeAttr("disabled");
                $("#" + btnName).html(word);
                clearInterval(flag);
            } else {
                $("#" + btnName).attr("disabled", true);
                $("#" + btnName).html(word + "(" + count + ")");
                count--;
            }
        }, 1000);
    },
    //兼容浏览器，关闭当前页面的代码
    closeWebPage: function () {
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                window.opener = null;
                window.close();
            } else {
                window.open('', '_top');
                window.top.close();
            }
        } else if (navigator.userAgent.indexOf("Firefox") > 0) {
            window.location.href = 'about:blank ';
        } else {
            window.opener = null;
            window.open('', '_self', '');
            window.close();
        }
    },
    deleteHtmlTag: function (str) {
        return str.replace(/<[^>]+>/g, "");
    }
}

